package com.intretech.umsin.code.generator.provider;


import com.intretech.umsin.code.generator.component.DataBaseType;
import com.intretech.umsin.code.generator.component.SchemaHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @ClassName: SchemaProviderFactory
 * @Description:
 * @author: 李启联
 * @date: 2020-01-22 09:52
 * @Copyright: Copyright (c), 2020
 */

@Service
public class SchemaProviderFactory {

    private static final Logger logger = LoggerFactory.getLogger(SchemaProviderFactory.class);

    @Autowired
    private SchemaHelper schemaHelper;

    /*动态注入 此写法更优雅*/
    Map<String,IDbSchemaProvider> schemaProvidersMap = new HashMap<String,IDbSchemaProvider>();
    public SchemaProviderFactory(List<IDbSchemaProvider> schemaProviders){
        for (IDbSchemaProvider schemaProvider : schemaProviders) {
            schemaProvidersMap.put(schemaProvider.dataBaseType().toString(),schemaProvider);
        }
    }

    /**
     * 创建具体实现者
     * @return
     */
    public IDbSchemaProvider createDbSchemaProvider() {
        IDbSchemaProvider schemaProvider = null;
        DataBaseType dataBaseType = null;
        try {
            dataBaseType = schemaHelper.getDataBaseType();
        } catch (Exception e) {
            logger.error("createDbSchemaProvider",e);
        }
        schemaProvider = schemaProvidersMap.get(dataBaseType.toString());
        return schemaProvider;

    }
}